package com.zonda.sampleretrofit2.model;


import android.text.TextUtils;
import android.util.JsonReader;

import java.io.IOException;
import java.util.ArrayList;

public class NewsModel extends AppBasicObject {

  private String pk;

  private String title;

  private String authorName;

  private String date;

  private String thumbnailMinPic;

  private String iconUrl;

  private ArrayList<NewsMediaModel> thumbnailMedias;

  @Override
  public boolean convertFrom(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
      final String name = jsonReader.nextName();
      if (TextUtils.equals(name, "pk")) {
        pk = jsonReader.nextString();
      } else if (TextUtils.equals(name, "title")) {
        title = jsonReader.nextString();
      } else if (TextUtils.equals(name, "auther_name")) {
        authorName = jsonReader.nextString();
      } else if (TextUtils.equals(name, "date")) {
        date = jsonReader.nextString();
      } else if (TextUtils.equals(name, "thumbnail_min_pic")) {
        thumbnailMinPic = jsonReader.nextString();
      } else if (TextUtils.equals(name, "thumbnail_medias")) {
        readThumbnailMedias(jsonReader);
      } else if (TextUtils.equals(name, "special_info")) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
          final String name1 = jsonReader.nextName();
          if (TextUtils.equals(name1, "icon_url")) {
            iconUrl = jsonReader.nextString();
          } else {
            jsonReader.skipValue();
          }
        }
        jsonReader.endObject();
      } else {
        jsonReader.skipValue();
      }
    }
    jsonReader.endObject();
    return true;
  }

  private void readThumbnailMedias(JsonReader jsonReader) throws IOException {
    jsonReader.beginArray();
    thumbnailMedias = new ArrayList<>();
    NewsMediaModel newsMediaModel;
    while (jsonReader.hasNext()) {
      newsMediaModel = new NewsMediaModel();
      newsMediaModel.convertFrom(jsonReader);
      thumbnailMedias.add(newsMediaModel);
    }
    jsonReader.endArray();
  }

  public String getPk() {
    return pk;
  }

  public void setPk(String pk) {
    this.pk = pk;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getAuthorName() {
    return authorName;
  }

  public void setAuthorName(String authorName) {
    this.authorName = authorName;
  }

  public String getDate() {
    return date;
  }

  public void setDate(String date) {
    this.date = date;
  }

  public String getThumbnailMinPic() {
    return thumbnailMinPic;
  }

  public void setThumbnailMinPic(String thumbnailMinPic) {
    this.thumbnailMinPic = thumbnailMinPic;
  }

  public ArrayList<NewsMediaModel> getThumbnailMedias() {
    return thumbnailMedias;
  }

  public void setThumbnailMedias(ArrayList<NewsMediaModel> thumbnailMedias) {
    this.thumbnailMedias = thumbnailMedias;
  }

  public String getIconUrl() {
    return iconUrl;
  }

  public void setIconUrl(String iconUrl) {
    this.iconUrl = iconUrl;
  }
}
